package com.metagurukul.informer.service;

import com.metagurukul.informer.dao.LoginUserDAO;
import com.metagurukul.informer.dao.RegisterUserDAO;
import com.metagurukul.informer.valueObjects.Users;
import java.sql.*;

public class RegisterUserService {
	
	Users users = null;
	RegisterUserDAO registeruserdao;
	LoginUserDAO loginuserdao;
	private String id;
	private String name;
	private String password;
	public RegisterUserService(String id, String name) {

		this.id = id;
		this.name = name;
	}
	public RegisterUserService(String id, String name,String password ) {

		this.id = id;
		this.name = name;
		this.password=password;
	}
	/**
	 * register as temp user and return auto generated password 
	 * @return0
	 * @throws SQLException
	 */
	public String doRegisterUser() throws SQLException{
		registeruserdao=new RegisterUserDAO();
		String password=registeruserdao.registerUser(id,name);
		return password;
	}
	/**
	 * check that if user is member or not
	 * @return
	 * @throws SQLException
	 */
	public boolean isUserExist() throws SQLException{
		loginuserdao=new LoginUserDAO();
		boolean isExist=false;
		try {
			isExist=loginuserdao.isExistingUser(id);
		} catch (Exception e) {
			throw new SQLException();
		}
		
		return  isExist;
	}
	/**
	 * update the old password
	 * @return
	 * @throws SQLException
	 */
	public boolean update() throws SQLException {
		registeruserdao=new RegisterUserDAO();
		if(password!=null && !password.equals("")){
			registeruserdao.updatePassword(id, password);
			return true;
		}
		return false;
	}
	/**
	 * check that if user is registered or not
	 * @return
	 * @throws SQLException
	 */
public boolean isTempUser()throws SQLException{
		
		loginuserdao=new LoginUserDAO();
		boolean isExist=false;
		isExist=loginuserdao.isTempUser(id);
		return  isExist;
	}
/**
 * delete user from temp user table 
 * @throws SQLException
 */
	public void deleteTempUser()throws SQLException{
		registeruserdao=new RegisterUserDAO();
		registeruserdao.deleteTempUser(id);
	}
}
